package com.ruoyi.web.controller.df;

import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.domain.vo.CommonVO;
import com.ruoyi.common.utils.file.FileUtils;
import com.ruoyi.df.domain.DensityFusionParam;
import com.ruoyi.df.domain.DensityFustionVO;
import com.ruoyi.web.server.service.impl.DensityDataFusionService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * </p>
 *
 * @author liuhl
 * @since 2024-06-05
 */

@RestController
@RequestMapping("/df/fusion")
@Api(value = "DensityDataFusionController", tags = "数据融合-大气密度数据融合")
@Slf4j
public class DensityDataFusionController extends BaseController {

    @Resource
    private DensityDataFusionService fusionService;

    //@ApiOperation(value = "返回存在数据的时间", notes = "传年 返回月;传年月 返回日")
    //@PostMapping("/time")
    //public R<List<Integer>> findTime(DensityFusionParam param) {
    //    List<Integer> resList = fusionService.findTime(param);
    //    return R.ok(resList);
    //}

    @ApiOperation(value = "返回存在数据的卫星列表", notes = "返回存在数据的卫星列表")
    @PostMapping("/menu")
    public R<List<CommonVO>> dateList(@RequestBody DensityFusionParam param) {
        List<String> res = fusionService.dateList(param);
        List<CommonVO> list = new ArrayList<>();
        for (String re : res) {
            list.add(new CommonVO(re,re));
        }
        return R.ok(list);
    }
    //
    //@ApiOperation(value = "返回卫星的列表", notes = "返回卫星的列表")
    //@GetMapping("/statellite")
    //public R<Set<CommonVO>> satellite(@ApiParam(value = "时间 yyyy-MM-dd-HH", name = "dataId") @RequestParam(value = "dataId",required = true) String dataId) {
    //    DensityFustionSatelliteVO densityFustionSatelliteVO = fusionService.satelliteByFile(dataId);
    //    Set<CommonVO> satellites = densityFustionSatelliteVO.getSatellites();
    //    return R.ok(satellites);
    //}
    //
    //@ApiOperation(value = "返回指定时次下卫星的列表", notes = "返回指定时次下卫星的列表")
    //@GetMapping("/statelliteByTime")
    //public R<List<CommonVO>> satelliteByTime(@ApiParam(value = "时间 yyyy-MM-dd-HH", name = "dataId") @RequestParam(value = "dataId",required = true) String dataId) {
    //    try {
    //        List<CommonVO> commonVOS = fusionService.satellitesByTime(dataId);
    //        return R.ok(commonVOS);
    //    } catch (ServerException e) {
    //        log.info(e.getMessage());
    //        return R.fail(e.getMessage());
    //    }
    //}


    //
    //@ApiOperation(value = "返回指定时次,指定卫星的分辨率列表", notes = "返回指定时次,指定卫星的分辨率列表")
    //@GetMapping("/interval")
    //public R<Set<String>> interval(@ApiParam(value = "资料id", name = "dataId") @RequestParam(value = "dataId",required = true) String dataId,
    //                               @ApiParam(value = "卫星数组", name = "sates") @RequestParam(value = "sates",required = true) List<String> sates) {
    //
    //    try {
    //        Set<String> res = fusionService.gridBySate(dataId,sates);
    //        return R.ok(res);
    //    } catch (ServerException e) {
    //        log.info(e.getMessage());
    //        return R.fail(e.getMessage());
    //    }
    //}


    @ApiOperation(value = "融合", notes = "融合")
    @PostMapping("/calc")
    public R<List<DensityFustionVO>> calc(@RequestBody DensityFusionParam param) throws Exception {
        List<DensityFustionVO> res = fusionService.fusionCalc(param);
        return R.ok(res);
    }

    @ApiOperation(value = "结果入库", notes = "结果入库")
    @PostMapping("/save")
    public R save(@RequestBody DensityFusionParam param) {
        try {
            boolean res = fusionService.resSave(param);
        } catch (Exception e) {
            log.error("结果入库",e);
            return R.fail(e.getMessage());
        }
        return R.ok();
    }

    @ApiOperation(value = "结果下载", notes = "结果下载")
    @PostMapping("/download")
    public void download(@RequestBody DensityFusionParam param,HttpServletResponse response) {
        try {
            File file = fusionService.download(param);
            response.setContentType(MediaType.APPLICATION_OCTET_STREAM_VALUE);
            FileUtils.setAttachmentResponseHeader(response, file.getName());
            FileUtils.writeBytes(file.getAbsolutePath(), response.getOutputStream());
            file.delete();
        }catch (Exception e) {
            log.error("下载失败",e);
        }
    }
}
